/*************************************************************************
    > File Name: progressbar.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Sun 16 Feb 2025 04:34:43 PM CST
 ************************************************************************/

#include"Progressbar.h"
#include<string.h>
#include<unistd.h>
#define length 101                                //缓冲区长度
#define Style '#'                                 //缓冲区填入的字符#

const char * lable = "|/-\\";                     //旋转光标

void ProcBar(double total,double current)
{
	 char bar[length];                            //缓冲区大小
	 memset(bar,'\0',sizeof(bar));                //清空缓冲区，都设置为0	
	 int cnt = 0;                                 
	 int len = strlen(lable);                     //len=4,旋转光标就四个字符"|/-\"
	 double rate = (current*100.0/total);         //rate当前下载量进度条百分比%
	 int loop_count = (int)rate;                  //当前循环次数，即下载量进度条百分比%
												  
	while(cnt<=loop_count)                        //需要加载下载量百分比%
	{
		printf("[%-100s][%.1lf%%][%c]\r",bar,rate,lable[cnt%len]);    
		//%-100s,左对齐，长度100；%.1if保留一位浮点数，%c打印旋转光标，计数器cnt模上字符长度
		fflush(stdout);                           //强制冲刷缓冲区
		bar[cnt++]=Style;                         //缓冲区放入新的字符#，然后累加
		usleep(20000);                            //usleep以微秒为单位，需加头文件<unistd.h>
	}
	printf("\n");
}

